<template>
  <div class="main_box" style="padding:10px">
    <div class="add_wrap">
      <div class="tit">
        <h2>{{ dataForm.classifyId ? '编辑商户分类(id:'+dataForm.classifyId+')' : '新增商户分类' }}</h2>
        <el-button type="success" plain size="small" icon="fa fa-mail-forward" @click="returnList">返回</el-button>
      </div>
      <el-form :model="dataForm" ref="dataForm" label-width="200px" :rules="roleRules">
        <el-row>
          <el-form-item label="分类名称" prop="classifyName">
            <RichEditor :richText="dataForm.classifyName" @getRichText="getRichText" placeholder="请输入分类名称"
              height="200px"
              menubar=""
              :toolbar="['bold  forecolor emoticons  image removeformat']"
              :plugins="['image imagetools  tabfocus  textcolor emoticons']"
              ></RichEditor>
          </el-form-item>
          <el-form-item label="商户" prop="businessId">
            <el-select v-model="dataForm.businessId" clearable filterable>
              <el-option v-for="item in businessList" :label="item.name" :value="item.id" :key="item.id">

              </el-option>
            </el-select>
          </el-form-item>
          <!-- <el-form-item label="关联科室" prop="officeId">
            <el-select v-model="dataForm.officeId">
              <el-option v-for="item in officeList" :value="item.id" :label="item.name" :key="item.id"></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="排序（倒序）" prop="sort">
            <el-input v-model="dataForm.sort" @input="handleSortInput">
            </el-input>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item>
            <el-button plain type="primary" @click="sureClick">保 存</el-button>
            <el-button plain @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>

      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import BusinessApi from "@/api/business/business";
import BusinessClassifyApi from '@/api/business/businessClassify';
import RichEditor from "../../components/RichEditor.vue";
// import OfficeApi from "@/api/institution/office";
// import ServerClassifyApi from "@/api/server/serverClassify"

export default {
  data() {
    return {
      dataForm: {
        modleId: "",
        classifyName: "",
        businessId: "",
        officeId:"",
        sort: "",
      },
      officeList:[],
      businessList: [],
      roleRules: {
        classifyName: [
          { required: true, message: "请输入分类名称", trigger: "focus" }
        ],
        businessId: [
          { required: true, message: "请选择商户", trigger: "blur" }
        ]
      }
    }
  },
  components: {
    RichEditor
  },
  mounted() {
    this.init()
  },
  methods: {
    init() {
      this.getBusinessList()
      if (this.$route.query.id) {
        this.dataForm.classifyId = this.$route.query.id
        this.getData()
      }
    },
    keydown(e) {
      if (e.keyCode == 32) {
        e.returnValue = false;
      }
    },
    async getData() {
      const {data} = await  BusinessClassifyApi.getBusinessClassifyInfo({id: this.dataForm.classifyId})
        this.$Utils.bindingBean(this.dataForm,data)
        // this.getOfficeList()
    },
    // async getOfficeList(){
    //   const hospitalId = this.businessList.find(item=>item.id==this.dataForm.businessId).hospitalId
    //   if(!hospitalId) return
    //   const {data} = await OfficeApi.getOfficeNames({hospitalId})
    //   this.officeList = data||[]
    // },
    async getBusinessList() {
      const res = await BusinessApi.listIdNameVo()
      this.businessList = res.data
    },
    // handleBusinessChange(){
    //   // this.officeList = []
    //   // this.dataForm.officeId = ''
    //   // if(!this.dataForm.businessId) return
    //   // this.getOfficeList()
    // },
    checkData() {
      const { dataForm } = this
      // if (!dataForm.classifyName) return
      return true
    },
    handleSortInput() {
      let { sort } = this.dataForm
      this.dataForm.sort = sort = sort.replace(/[^0-9]|^0/g, "")
      if (sort > 999999) {
        this.dataForm.sort = ""
      }
    },
    getRichText(val) {
      this.dataForm.classifyName = val
    },
    sureClick() {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (!this.checkData()) return
          this.$confirm(message.MSG_CONFIRM_SAVE_ALTER, message.TITLE_PROMPT, {})
            .then(() => {
              if (this.dataForm.classifyId) {
                this.editData();
              } else {
                this.addData();
              }
            });
        }
      })
    },
    addData() {
      let param = {
        classifyName: this.dataForm.classifyName,
        businessId: this.dataForm.businessId,
        sort: this.dataForm.sort
      };

      BusinessClassifyApi.addBusinessClassify(param).then(res => {
        this.$Utils.notifySuccess()
        this.returnList();
      })
    },
    editData() {
      let param = {
        classifyId: this.dataForm.classifyId,
        classifyName: this.dataForm.classifyName,
        businessId: this.dataForm.businessId,
        sort: this.dataForm.sort
      };
      BusinessClassifyApi.editBusinessClassify(param)
        .then(data => {
          this.$Utils.notifySuccess()
          this.returnList();
        })
    },
    returnList() {
      this.$store.dispatch('delView', this.$route);
      this.$router.push({ path: '/business/businessClassifyManagement' });
    }
  },

}
</script>
<style lang="scss" scoped>
@import "@/assets/css/views/add.scss";
</style>

<style lang="scss" scoped>
.upload-img {
  &::v-deep .el-upload-dragger {
    display: flex;
    justify-content: space-around;
    align-items: center;

    .tip {
      padding-bottom: 30px;
    }
  }
}
</style>

